(function() {

	angular.module('cf-content')
		.directive('brand', ['$timeout', "$window", function($timeout, $window, EVENT) {
			return {
				restrict: 'AE',
				templateUrl: 'app/page/brandmanage/brandmanage.html',
				// replace:true,
				controller: brandmanage,
				controllerAs: 'brandCtrl'
			}
		}])
		.controller('BrandModalInstanceCtrl', BrandModalInstanceCtrl)

	BrandModalInstanceCtrl.$inject = ['$scope', '$uibModalInstance'];

	function BrandModalInstanceCtrl($scope, $uibModalInstance) {


		$scope.ok = function() {
			$uibModalInstance.close('delete');
		};

		$scope.cancel = function() {
			$uibModalInstance.dismiss('cancel');
		};
	}

	brandmanage.$inject = ['$scope', '$uibModal', '$q', 'Api']

	function brandmanage($scope, $uibModal, $q, Api) {
		var vm = this;
		//pagination
		vm.pagination={
			max_size:5,
			page_size:20,
			page_no:1,
			total_item:2
		}
		vm.initials = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

		QueryBrandList();

		vm.toggleAllCheckbox = function() {
			angular.forEach(vm.brands, function(value) {
				value['$selected'] = vm.toggleCheckbox;
			})
			if(!vm.toggleCheckbox){
              vm.showOperationButton=false;
            }
		}
		vm.showOperation=function(data){
            for(var i in vm.brands){
                if(vm.brands[i].$selected){
                    vm.showOperationButton=true;
                    return;
                }
            }
            vm.toggleCheckbox=false;
            vm.showOperationButton=false;
        }

		vm.paginationChange=function(){
			QueryBrandList();
		}
		function QueryBrandList() {
			Api.Brand.Query({page_size:vm.pagination.page_size,page_no:vm.pagination.page_no}).$promise.then(function(data) {
				vm.brands = data.data.brand_list;
				vm.pagination.total_item=data.data.total_item;
				angular.forEach(vm.brands, function(value) {
					value.first_letter = value.first_letter.toUpperCase()
					value['$selected'] = false;
				})
				if(data.code!=0)
					vm.InfoModel('sm', '加载失败','error');
			})
		}
		vm.resetFilter = function(data) {
			$scope.brandFilter = '';
		}

		vm.addBrand = function() {
			vm.insertedata = {
				$selected: false,
				name: '',
				first_letter: '',
				description: '',
				field_id: '',
				field_name: '',
				country: '',
				factory_name: '',
				contact: ''
			};
			vm.brands.push(vm.insertedata);
		};

		vm.deleteAll = function() {
			var selectedArray = [],
				promiseList = [];
			for (var i in vm.brands) {
				if (vm.brands[i].$selected) {
					selectedArray.push(vm.brands[i].id);
				}
			}
			if (selectedArray.length < 1) {
				vm.InfoModel('sm', '请选择要操作的项')
			} else {
				vm.EnsureModel('sm', '选中项', function(data) {
					for (var j in selectedArray) {
						promiseList.push(Api.Brand.Delete({
							brand_id: selectedArray[j]
						}).$promise)
					}
					$q.all(promiseList).then(function(data) {
						QueryBrandList()
						if(data.code==0)
							vm.InfoModel('sm', '操作成功','success');
					})
				})

			}
		}
		vm.showFieldName = function(data) {
			if (data.field_id) {
				return data.field_name;
			} else {
				return '请选择'
			}
		}
		vm.saveBrand = function(data, id,field_id) {
			if (!!id) {
				Api.Brand.Update(angular.extend({}, data, {
					brand_id: id,
					field_id:field_id
				})).$promise.then(function(data) {
					if(data.code==0)
						vm.InfoModel('sm', '操作成功','success');
					else if(data.code==2)
						vm.InfoModel('sm', '该品牌名已存在','error');
					QueryBrandList()
				})
			} else {
				Api.Brand.Put(angular.extend({}, data, {
					field_id:field_id
				})).$promise.then(function(data) {
					if(data.code==0)
						vm.InfoModel('sm', '操作成功','success');
					else if(data.code==2)
						vm.InfoModel('sm', '该品牌名已存在','error');
					QueryBrandList()
				})
			}
		}

		vm.checkData = function(data, brand) {
				if (!data) {
					return '内容不能为空';
				}
				if(brand){
					if(!brand.field_id){
						vm.InfoModel('sm', '请选择行业');
						return '';
					}

				}
			}
			//model
		vm.EnsureModel = function openModel(size, item, callback) {
			var modalInstance = $uibModal.open({
				animation: true,
				templateUrl: 'EnsureModel',
				controller: 'EnsureModelCtrl',
				size: size,
				resolve: {
					item: function() {
						return '删除'+item;
					}
				}
			});

			modalInstance.result.then(callback);
		}

		vm.InfoModel = function openModel(size, info,type,callback) {
			var modalInstance = $uibModal.open({
				animation: true,
				templateUrl: 'InfoModel',
				controller: 'InfoModelCtrl',
				size: size,
				resolve: {
					info: function() {
						return info;
					},
					type:function(){
						return type;
					}
				}
			});

			modalInstance.result.then(callback);
		}

		vm.SelectModel = function openModel(size, field, callback) {
			var modalInstance = $uibModal.open({
				animation: true,
				templateUrl: 'SelectModel',
				controller: 'SelectModelCtrl',
				size: size,
				resolve: {
					field: function() {
						return field;
					}
				}
			});

			modalInstance.result.then(callback);
		}

		vm.selectField = function(brand,visible) {
			if(!visible)
				return ;
			vm.SelectModel('lg', brand, function(data) {
				if (data) {
					brand.field_name = data.name;
					brand.field_id = data.id;
				} else {
					vm.InfoModel('sm', '选择的内容为空','error')
				}
			})
		}


		vm.removeBrand = function(data, id) {
			vm.EnsureModel('sm', data.name, function() {
				if (data) {
					Api.Brand.Delete({
						brand_id: data.id
					}).$promise.then(function(data) {
						if(data.code==0)
							vm.InfoModel('sm', '操作成功','success')
							vm.brands.splice(id, 1)
					})
				}
			})
		}
		
		// cancel all changes
		vm.cancel = function(rowform,brand) {
			rowform.$cancel();
			if(vm.insertedata == brand){
				vm.brands.pop();
			}
		};
	}

})();